/**
 * @param {number} capacity
 */
var LRUCache = function (capacity) {
  this.capacity = capacity
  this.map = new Map()
};

/** 
 * @param {number} key
 * @return {number}
 */
LRUCache.prototype.get = function (key) {
  //如果哈希表中不存在这个key，返回-1
  if (!this.map.has(key)) return -1

  //如果哈希表中存在这个key，需要删去原来的key-value，重新插入
  const value = this.map.get(key)
  this.map.delete(key)
  this.map.set(key, value)
  //返回value
  return value
};

/** 
 * @param {number} key 
 * @param {number} value
 * @return {void}
 */
LRUCache.prototype.put = function (key, value) {
  //如果哈希表中存在这个key，则先删去这个key-value
  if (this.map.has(key)) {
    this.map.delete(key)
  } else if (this.map.size >= this.capacity) {//如果再插入一个key-value，哈希表超限，则删除最久未使用的key-value
    const firstKey = this.map.keys().next().value
    this.map.delete(firstKey)
  }
  //插入key-value
  this.map.set(key, value)
};
/** 
 * Your LRUCache object will be instantiated and called as such:
 * var obj = new LRUCache(capacity)
 * var param_1 = obj.get(key)
 * obj.put(key,value)
 */